﻿using HCLSMod;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SY280DRIVER
{
    public class SY280CFGINI : IniFile
    {
        public static string _INIFileName = System.IO.Directory.GetCurrentDirectory()
            + "\\SY280DriverCFG.ini";

        public string CFGSegName = "SY280Driver";

        public SY280CFGINI(string sFileName) : base(sFileName)
        {
        }
        public SY280CFGINI() : base(_INIFileName)
        {
        }

        public string SerialPort
        {
            get
            {
                return this.ReadIniKeys(CFGSegName, "SerialPort", "COM1");
            }
            set
            {
                this.WriteIniKeys(CFGSegName, "SerialPort", value.ToUpper());
            }
        }

        public bool AutoStar
        {
            get
            {
                int iValue = 0;
                return int.TryParse(this.ReadIniKeys(CFGSegName, "AutoStar", ""), out iValue) && (iValue == 1);
            }
            set
            {
                if (value)
                    this.WriteIniKeys(CFGSegName, "AutoStar", "1");
                else
                    this.WriteIniKeys(CFGSegName, "AutoStar", "0");
            }
        }

        public int ServerPort
        {
            get
            {
                int iValue = 0;
                if (int.TryParse(this.ReadIniKeys(CFGSegName, "ServerPort", "5875"), out iValue))
                    return iValue;
                return 5875;
            }
            set
            {
                if (value>1000)
                    this.WriteIniKeys(CFGSegName, "ServerPort", value.ToString());
                else
                    this.WriteIniKeys(CFGSegName, "ServerPort", "5875");
            }
        }

        public int PLCAddress
        {
            get
            {
                int iValue = 0;
                if (int.TryParse(this.ReadIniKeys(CFGSegName, "PLCAddress", "1"), out iValue))
                    return iValue;
                return 1;
            }
            set
            {
                if ((value > 0) && (value < 255))
                    this.WriteIniKeys(CFGSegName, "PLCAddress", value.ToString());
                else
                    this.WriteIniKeys(CFGSegName, "PLCAddress", "1");
            }
        }


    }
}
